package zhang.automobileservice;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import adapter.CarFriendAdapetr;
import bean.CarFriendBean;

import utils.MyUrl;
import utils.RequestQueueUtils;

public class MypublishActivity extends FarterActivity implements View.OnClickListener {

    private ImageButton finish;
    private ImageButton ib_publish;
    private ListView lv_mypublish;
    private ArrayList<CarFriendBean> arrayList;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mypublish);
        id = getSharedPreferences("TAGtext",MODE_PRIVATE).getString("id","1");
        getData();
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.finish:
                finish();
                break;
            case R.id.ib_publish:
                break;
        }
    }
    private void getData() {
        String url = MyUrl.Font_Url+MyUrl.queryByUser+"?id="+id;
        RequestQueueUtils.exectueJsonRequest(getApplicationContext(), url, new com.android.volley.Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                arrayList = new ArrayList<CarFriendBean>();
                Log.e("------",o.toString());
                try {
                    if("0".equals(o.getString("code"))){
                        JSONArray obj = o.getJSONArray("data");
                        Log.e("+++++",obj.toString());
                        for(int i=0;i<obj.length();i++){
                            JSONObject jsonObject = obj.getJSONObject(i);
                            String username = jsonObject.getString("username");
                            String createDate = jsonObject.getString("createDate");
                            String imgs = jsonObject.getString("imgs");
                            String id = jsonObject.getInt("id")+"";
                            String headPic = jsonObject.getString("headPic");
                            String likeCount = jsonObject.getInt("likeCount")+"";
                            String content = jsonObject.getString("content");
                            CarFriendBean friendBean = new CarFriendBean(id,content,imgs,username,headPic,likeCount,createDate);
                            arrayList.add(friendBean);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.e("------",arrayList.toString());
                lv_mypublish.setAdapter(new CarFriendAdapetr(getApplicationContext(),arrayList));
            }
        });
    }
    private void initView() {
        finish = (ImageButton) findViewById(R.id.finish);
        ib_publish = (ImageButton) findViewById(R.id.ib_publish);
        lv_mypublish = (ListView) findViewById(R.id.lv_mypublish);

        finish.setOnClickListener(this);
        ib_publish.setOnClickListener(this);
    }
}
